home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4237 < prev    next >
Encoding:
Text File  |  1996-08-06  |  5.1 KB  |  179 lines

  1. Path: ix.netcom.com!netnews
  2. From: mearrin@ix.netcom.com(Michael Arrington )
  3. Newsgroups: comp.lang.c++
  4. Subject: C++ help--probably a simple answer (short listing attached)
  5. Date: 29 Jan 1996 03:07:01 GMT
  6. Organization: Netcom
  7. Message-ID: <4ehdkl$mj0@ixnews7.ix.netcom.com>
  8. NNTP-Posting-Host: mon-ca5-12.ix.netcom.com
  9. X-NETCOM-Date: Sun Jan 28  7:07:01 PM PST 1996
  10.  
  11. Hi,
  12.  
  13. I have a question about C++.  I'm just learning C++ but I've been
  14. writing programs in other languages for a number of years.  In this
  15. problem, I'm creating a class to represent a 2D cellular automaton
  16. (like Conway's Life). Here's the (partial) declaration and
  17. implementation of the class:
  18.  
  19. *******************
  20. ****FROM CELL.H****
  21. *******************
  22.  
  23. class CCellAuto
  24. {
  25. public:
  26.     // Public constructors & destructors
  27.     CCellAuto();                                // Default constructor
  28.     CCellAuto(int xSize, int ySize);            // Overloaded
  29. Constructor
  30.     ~CCellAuto();                               // Default Destructor
  31.     // Public accessor methods
  32.     void BumpGen();
  33.     void BumpPop();
  34.     int GetGen() const;
  35.     int GetPop() const;
  36.     void RandomizeCells();
  37.     void SetRules(int deathLow, int deathHigh, int birthLow, int
  38. birthHigh);
  39.     void UpdateCells();                                                
  40.     void ResizeArrays(int xSize, int ySize);
  41.  
  42. private: 
  43.     // Private data members
  44.     int xSize;              // X dimension of arrays
  45.     int ySize;              // Y dimension of arrays
  46.     int cell[1][1];         // Array holding live cells
  47.     int old[1][1];          // Temp array
  48.     int deathLow;           // Birth/Death rules
  49.     int deathHigh;
  50.     int birthLow;
  51.     int birthHigh;
  52.     int currGen;            // # of current generation
  53.     int currPop;            // # of cells alive
  54.     int wrap;               // Wrap to other side of array?
  55. };
  56.  
  57. *********************
  58. ****FROM CELL.CPP****
  59. *********************
  60.  
  61. // Default constructor
  62. CCellAuto::CCellAuto()
  63. {
  64.     wrap = 1;                           // Set wrap to on
  65.     currPop = 0;                        // Initialize currPopulation
  66.     currGen = 1;                        // Initialize currGeneration
  67.     ResizeArrays(10, 10);               // Set default array size
  68.     SetRules(2, 4, 2, 4);               // Set default rules
  69.     RandomizeCells();                   // Randomize cells
  70. }
  71.  
  72. // Overloaded Constructor
  73. CCellAuto::CCellAuto(int x, int y)
  74. {
  75.     wrap = 1;                           // Set wrap to on
  76.     currPop = 0;                        // Initialize currPopulation
  77.     currGen = 1;                        // Initialize currGeneration
  78.     ResizeArrays(x, y);                 // Size arrays to X by Y
  79.     SetRules(2, 4, 2, 4);               // Set default rules
  80.     RandomizeCells();                   // Randomize cells
  81. }
  82.  
  83. // Default Destructor
  84. CCellAuto::~CCellAuto()
  85. {
  86. }
  87.  
  88. // BumpGen()
  89. void CCellAuto::BumpGen()
  90. {
  91.     currGen++;
  92. }
  93.  
  94. // BumpPop()
  95. void CCellAuto::BumpPop()
  96. {
  97.     currPop++;
  98. }         
  99.  
  100. // GetGen()
  101. int CCellAuto::GetGen() const
  102. {
  103.     return currGen;
  104. }
  105.  
  106. // GetPop()
  107. int CCellAuto::GetPop() const
  108. {
  109.     return currPop;
  110. }    
  111.  
  112. // RandomizeCells()
  113. void CCellAuto::RandomizeCells()
  114. {
  115.      // Omitted...Randomly sets cell[][] elements to either 1 or 0
  116. }               
  117.  
  118. // ResizeArrays()
  119. void CCellAuto::ResizeArrays(int x, int y)
  120. {
  121.      // Omitted...Sets cell[][] and old[][] to dimensions x and y
  122. }
  123.  
  124. // SetRules()
  125. void CCellAuto::SetRules(int a, int b, int c, int d)
  126. {
  127.     deathLow = a;
  128.     deathHigh = b;
  129.     birthLow = c;
  130.     birthHigh = d;
  131. }
  132.  
  133. // UpdateCells()
  134. void CCellAuto::UpdateCells()
  135. {
  136.      // Omitted...copies cell[][] to old[][] and creates next iteration
  137.      // in new[][].  Calls BumpGen() when done.
  138. }
  139.  
  140. Whew!  I omitted a lot of that...but I wanted to give enough to let you
  141. see any problems I might have created in it.  Now, when I declare a
  142. CCellAuto object [[like this -- CCellAuto *Life1 = new CCellAuto(20,
  143. 20); ]]and try to access a member function [[like this -- cout <<
  144. Life1->GetPop() << endl; ]] I get uninitialized variables.  That's
  145. weird because I initialize them in the constructor.  Now, if I were to
  146. call Life1->BumpPop() first...the GetPop() function returns the correct
  147. number.  Why?  Here's my main() listing:
  148.  
  149. *********************
  150. ****FROM LIFE.CPP****
  151. *********************
  152.  
  153. 01   void main()
  154. 02   {
  155. 03       CCellAuto *Life1 = new CCellAuto(20,20);
  156. 04       if (Life1 == 0)
  157. 05           cout << "cannot allocate memory\n";
  158. 06       //Life1->BumpPop();
  159. 07       cout << "Life1 Pop: " << Life1->GetPop() << endl;
  160. 08       cout << "Life1 Gen: " << Life1->GetGen() << endl;
  161. 09       delete Life1;
  162. 10   }
  163.  
  164.  
  165. As listed, lines 07 and 08 return 0 values for currPop and currGen.  If
  166. you remove the comment marks from line 06, line 07 will return the
  167. actual (albeit incremented) value of 1. I have no idea what is going on
  168. here.  I don't think it's my logic--I've written this same program
  169. before in BASIC and some other languages.  Any suggestions as to what's
  170. going on?  Please help!
  171.  
  172. If you have any answers, please e-mail to MEArrin@ix.netcom.com unless
  173. you feel it would be beneficial to the forum.  Thanks in advance for
  174. your help.
  175.  
  176. Mike
  177.  
  178. BTW:  I'm using MSC++ v7.0 if that's a help.
  179.